<?php
class Zone_View extends Zone_Config{
	public $__Data 		= array();
	public $_JSON 		= array();
	protected $_CACHE 	= array();
	public $contentType = 'HTML';
		
	public function setLayoutJSON( $bool ){
		$this->contentType = $bool ? 'JSON' : 'HTML';
	}
	
	public function set( $str ){
		return isset($this->__Data[$str]);
	}
	
	public function setDataJSON( $data ){
		$this->_JSON = array_merge( $this->_JSON, (array)$data );
	}	

	public function get( $str ){
		return $this->__Data[$str];
	}
	
	public function e( $str, $before='',$after='' ){
		if( isset($this->__Data[$str])){
			echo $before.($this->__Data[$str]).$after;
		}else{
			echo '';
		}	
	}
	
	public function setLayout( $layout ){
		$this->layoutPath = ROOT.'/apps/layouts/'.$layout.'.phtml';
	}
	
	//set module
	public function setLanguage( $language ){
		$this->language = $language;	
	}
	
	//set module
	public function setModule($module){
		$this->module = $module;	
	}

	//set controller
	public function setController($controller){
		$this->controller = $controller;	
	}

	//set module
	public function setAction($action){
		$this->action = $action;	
	}
	
	public function getActionName(){
		return strtolower($this->action).'Action';
	}
	
	public function getControllerName(){
		return $this->getModuleName().ucfirst(strtolower($this->controller)).'Controller';
	}
	
	public function getModuleName(){
		return ucfirst(strtolower($this->module));
	}
	
	
	
	/* Parse contents */	
	public function getContent( $cache = false ){
		$cache_path = 'apps/caches/contents/';
		$cache_file_name = urlencode(ZONE_FULL_URL);	

		//find from cache if need
		if( $cache && file_exists($cache_path.$cache_file_name) ){
			return file_get_contents($cache_path.$cache_file_name);		
		}	

		if( isset($this->overWriteHTML) ){
			$source = $this->overWriteHTML;
		}else{	
			$root_view = ROOT.'/apps/modules/'.$this->getModuleName().'/views/';
			ob_start();
			if( $this->viewName ){
				include $root_view.'/'.$this->viewName.'.phtml';
			}else{
				include $root_view.ucfirst(strtolower($this->controller)).ucfirst(strtolower($this->action)).'.phtml';	
			}
			$source = ob_get_contents();
			ob_end_clean();
		}

		if( $cache ){
			$f = @fopen($cache_path.$cache_file_name,'w+');		
			@fputs($f,$source);
			@fclose($f);
		}
		return $source;	
	}

	public function showContent( $cache = false ){
		echo $this->getContent();
	}
	
	/* Cache */
	public function startCache( $name ){
		if( !isset($this->_CACHE[$name])){
			$this->_CACHE[$name] = ob_get_length();
		}	
	}

	public function stopCache( $name ){
		if( isset($this->_CACHE[$name])){
			$str = ob_get_contents();
			$str = substr($str,$this->_CACHE[$name]);
			$f = fopen('cache/'.base64_encode($name),'w');
			fputs($f,$str);
			fclose($f);			
			unset($this->_CACHE[$name]);
		}		
	}
	
	public function getContentCache( $name ){
		return @file_get_contents('cache/'.base64_encode($name));
	}

	public function getCache( $name ){
		if( file_exists('cache/'.base64_encode($name)) ){
			echo $this->getContentCache( $name );
			return true;
		}else{
			return false;
		}	
	}
	
	public function removeCache( $name ){
		@unlink('cache/'.base64_encode($name));
		unset($this->_CACHE[$name]);
	}	
	
	
	
	
	

	public function loadAction( $module, $controller='index', $action='index', $language ='', $args = array(), $cache = false ){

		$app = new Zone_App( BASE_URL );
		$app->setModule( $module );
		$app->setController( $controller );
		$app->setAction( $action );
		$app->setLanguage( $language );
		$app->setParams( $args );
		$app->setParams(array(
			parent_module 		=> $this->module,
			parent_controller 	=> $this->controller,
			parent_action 		=> $this->action
		));
		
		$source = $app->run(FALSE);
		
		if( $cache ){
			$f = @fopen($cache_path.$cache_file_name,'w+');		
			@fputs($f,$source);
			@fclose($f);
		}
		
		return $source;
	}
	
	public function showAction( $module, $controller='index', $action='index', $language='', $args = array(), $cache = false ){
		echo $this->loadAction($module, $controller, $action, $language, $args, $cache);
	}
	
	//show view
	public function display(){
		if( $this->layoutPath !== NULL && file_exists( $this->layoutPath ) ){
			ob_start();
			require $this->layoutPath;
			$source = ob_get_contents();
			ob_end_clean();
		}else{
			$source = $this->getContent();
		}
		if( $this->contentType == 'JSON'){
			$data = array( content	=> $source );
			$data = array_merge( $this->_JSON, $data );
			//var_dump( $data );
			echo json_encode( $data );
			//echo  json_last_error();
		}else{
			echo $source;
		}		
	}

	/*
		Basic method for Zone_View	
	*/

	public function translate(){
		$args = func_get_args();	
		$language = strtolower($this->language);
		if( count( $args ) == 0 )
			return NULL;
		
		if( !is_array($this->_dataLang) ){
			$lang_default_dir = ROOT.'/apps/modules/'.ucfirst(strtolower($this->module)) .'/languages/default.php';		
			
			$module_data = array();
			$lang_module_dir  = ROOT.'/apps/modules/'.ucfirst(strtolower($this->module)) .'/languages/'.strtolower($language).'.php';		
				
			//check
			if( file_exists($lang_default_dir) )
				$default_data 	= include $lang_default_dir;
			if( file_exists($lang_module_dir) )		
				$module_data	= include $lang_module_dir;
			
			//merger
			$this->_dataLang = array_merge( (array)$default_data, (array)$module_data );			
		}
		
		$str = func_get_arg(0);
		array_shift( $args );
		
		$str = isset($this->_dataLang[$str]) ? $this->_dataLang[$str] : $str;
		return call_user_func_array('vsprintf', array( $str, $args ) );		
	}
	
	public function placeHolder( $index ){
		if( !$this->__placeHolder){
			return '';
		}	
		if(!isset( $this->__placeHolder[$index] )){
			return '';
		}
		return implode('',$this->__placeHolder[$index]);
	}

	public function getModule(){
		return $this->module;
	}
	
	public function getController(){
		return $this->controller;
	}
	
	public function getAction(){
		return $this->action;
	}	
	
	public function is( $module, $controller='index', $action='index', $language=NULL ){
		return ( strtolower($this->module) == strtolower($module)
			AND strtolower($this->controller) == strtolower($controller)
			AND strtolower($this->action) == strtolower($action)
			AND ( $language === NULL || strtolower($this->language) == strtolower($language) ));
	}	
	
	public function hasRole( $role ){
		return $this->_Controller->hasRole( $role );
	}	
}